﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using NCS.Library.Utilities;
using NCS.Module.CMS.Library;
using NCS.Library.Components.Portal;
using NCS.Library;
using NCS.Library.Components;
using NCS.Module.CMS.Library.Zones;
using NCS.Library.Components.Security;

namespace NCS.Module.CMS.Web.HomePage
{
    public partial class Header : CMSWebControl
    {
        //protected DataTable dtPortal;
        protected string topmenu = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            #region menu1

            //Menu1.Items.Clear();
            DataTable dt = ZoneController.GetZonesVisbleInTab(0);
            
            if (dt != null && dt.Rows.Count > 0)
            {
                /* begin add phongla 2012_08_07 */
                /* Chỉ lấy 5 menu cấp cha */
                int count_menu = 0;
                if (dt.Rows.Count > 5) count_menu = 5; else count_menu = dt.Rows.Count;
                /* end add phongla 2012_08_07 */

                /* begin modify phongla 2012_08_07 */
                //old code for (int i = 0; i < dt.Rows.Count; i++)
                for (int i = 0; i < count_menu; i++)
                /* end modify phongla 2012_08_07 */   
                {
                    int zone_authorize = ConvertUtility.ToInt32(dt.Rows[i]["Zone_Authorize"].ToString(), 0);
                    if (zone_authorize > 0)
                    {
                        if (!this.Request.IsAuthenticated)
                            continue;
                        else{

                            if (this.CurrentUser.UserType == 0 && zone_authorize > 1) {
                                continue;
                            }
                        }
                    }

                    int zoneid = NCS.Library.Utilities.ConvertUtility.ToInt32(dt.Rows[i]["Zone_ID"].ToString());
                    int tabid = NCS.Library.Utilities.ConvertUtility.ToInt32(dt.Rows[i]["Portal_TabControlID"].ToString());
                    int authorize = NCS.Library.Utilities.ConvertUtility.ToInt32(dt.Rows[i]["Zone_Authorize"], 0);
                    if (zoneid != 0 && (authorize == 0 || (authorize <= 1 && this.CurrentUser != null && this.CurrentUser.UserType == 0) || (authorize <= 2 && this.CurrentUser != null && this.CurrentUser.UserType == 1)))
                    {
                        string url = string.Empty;// BuildUrlByZoneSubPortal(AppEnv.PortalId(), zoneid.ToString(), tabid.ToString());
                        if (dt.Rows[i]["Zone_Url"] != null)
                            url = dt.Rows[i]["Zone_Url"].ToString();
                        if (url.Length == 0)
                            url = BuildUrlByZoneSubPortal(AppEnv.PortalId(), zoneid.ToString(), tabid.ToString());

                        /* begin modify phongla 2012_08_07 */
                        //code bi thay the
                        //this.topmenu += "<li><a href=\"" + url + "\">" + dt.Rows[i]["Zone_Name"].ToString() + "</a>";
                        //code duoc thay the
                        this.topmenu += "<li><a class='menuitem' href=\"" + url + "\">" + dt.Rows[i]["Zone_Name"].ToString() + "</a>";
                        /* end modify phongla 2012_08_07 */
                        this.topmenu += BuildZoneMenu(zoneid, AppEnv.PortalId());
                        this.topmenu += "</li>";
                    }
                }
            }

            #endregion menu1

            #region rpt_foot

            DataTable dt_foot = ZoneController.GetZonesVisbleInRightBottom();
            /* begin modify phongla 2012_08_08 */
            /* code cu 
            dt_foot.DefaultView.Sort = "Zone_Priority desc"; */
            /* thay the bang */
            dt_foot.DefaultView.Sort = "Zone_Priority asc";
            /* end modify phongla 2012_08_08 */
            this.rpt_foot.DataSource = dt_foot;
            this.rpt_foot.ItemDataBound += new RepeaterItemEventHandler(rpt_foot_ItemDataBound);
            this.rpt_foot.DataBind();

            #endregion rpt_foot
        }

        private string BuildZoneMenu(int zoneparent, string portalid)
        {
            string retval = string.Empty;

            DataTable dt = ZoneController.GetZones(zoneparent, portalid);
            if (dt != null && dt.Rows.Count > 0)
            {
                retval += "<ul>";
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    int zoneid = NCS.Library.Utilities.ConvertUtility.ToInt32(dt.Rows[i]["Zone_ID"].ToString());
                    int tabid = NCS.Library.Utilities.ConvertUtility.ToInt32(dt.Rows[i]["Portal_TabControlID"].ToString());
                    if (zoneid != zoneparent)
                    {
                        string url = string.Empty;
                        if (dt.Rows[i]["Zone_Url"] != null)
                            url = dt.Rows[i]["Zone_Url"].ToString();
                        if(url.Length == 0)
                            url = BuildUrlByZoneSubPortal(portalid, zoneid.ToString(), tabid.ToString());

                        retval += "<li><a href=\"" + url + "\">" + dt.Rows[i]["Zone_Name"].ToString() + "</a>";
                        retval += BuildZoneMenu(zoneid, portalid);
                        retval += "</li>";
                    }
                }
                retval += "</ul>";
            }

            return retval;
        }

        private void rpt_foot_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            DataRowView row = (DataRowView)e.Item.DataItem;
            if (row != null)
            {
                int tabid = NCS.Library.Utilities.ConvertUtility.ToInt32(row["Portal_TabControlID"].ToString());

                if ((tabid == 155 || tabid == 181) && this.Request.IsAuthenticated)
                    e.Item.Visible = false;

                HyperLink link = (HyperLink)e.Item.FindControl("hplk");

                int zone_authorize = ConvertUtility.ToInt32(row["Zone_Authorize"].ToString(), 0);
                if (zone_authorize > 0)
                {
                    if (!this.Request.IsAuthenticated)
                    {
                        e.Item.Visible = false;
                        return;
                    }
                    else
                    {

                        if (this.CurrentUser.UserType == 0 && zone_authorize > 1)
                        {
                            e.Item.Visible = false;
                            return;
                        }
                    }
                }

                int zoneid = NCS.Library.Utilities.ConvertUtility.ToInt32(row["Zone_ID"].ToString());
                
                int authorize = NCS.Library.Utilities.ConvertUtility.ToInt32(row["Zone_Authorize"], 0);
                if (zoneid != 0 && ((authorize == 0 && this.Request.IsAuthenticated == false) || (authorize <= 1 && this.CurrentUser.UserType == 0) || (authorize <= 2 && this.CurrentUser.UserType == 1)))
                {
                    string url = string.Empty;// BuildUrlByZoneSubPortal(AppEnv.PortalId(), zoneid.ToString(), tabid.ToString());
                    if (row["Zone_Url"] != null)
                        url = row["Zone_Url"].ToString();
                    if (url.Length == 0)
                        url = BuildUrlByZoneSubPortal(AppEnv.PortalId(), zoneid.ToString(), tabid.ToString());

                    link.NavigateUrl = url;
                    link.Text = row["Zone_Name"].ToString();
                }
                else
                {
                    e.Item.Visible = false;
                    return;
                }
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            if (this.Request.IsAuthenticated)
            {
                DataTable tbl = CMS.Library.Zones.ZoneController.GetZones(0, Convert.ToInt32(AppEnv.PortalId()), 4);
                this.rpt_menuAdmin.DataSource = tbl;
                this.rpt_menuAdmin.DataBind();
            }
        }

        protected void rpt_menuAdmin_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            DataRowView row = (DataRowView)e.Item.DataItem;
            if (row != null)
            {
                int zone_authorize = ConvertUtility.ToInt32(row["Zone_Authorize"].ToString(), 0);
                if (zone_authorize > 0)
                {
                    if (!this.Request.IsAuthenticated)
                    {
                        e.Item.Visible = false;
                        return;
                    }
                    else
                    {
                        if (this.CurrentUser.UserType == 0 && zone_authorize > 1)
                        {
                            e.Item.Visible = false;
                            return;
                        }
                    }
                }
                else if (this.Request.IsAuthenticated)
                {
                    e.Item.Visible = false;
                    return;
                }

                int zoneid = NCS.Library.Utilities.ConvertUtility.ToInt32(row["Zone_ID"].ToString());
                int tabid = NCS.Library.Utilities.ConvertUtility.ToInt32(row["Portal_TabControlID"].ToString());
                int authorize = NCS.Library.Utilities.ConvertUtility.ToInt32(row["Zone_Authorize"], 0);
                if (zoneid != 0 && ((authorize == 0 && this.Request.IsAuthenticated == false) || (authorize <= 1 && this.CurrentUser.UserType == 0) || (authorize <= 2 && this.CurrentUser.UserType == 1)))
                {
                    if (zoneid != 0)
                    {
                        string url = string.Empty;// BuildUrlByZoneSubPortal(AppEnv.PortalId(), zoneid.ToString(), tabid.ToString());
                        if (row["Zone_Url"] != null)
                            url = row["Zone_Url"].ToString();

                        if (url.Length == 0)
                            url = BuildUrlByZoneSubPortal(AppEnv.PortalId(), zoneid.ToString(), tabid.ToString());

                        HyperLink link = (HyperLink)e.Item.FindControl("link_menu");
                        link.Text = row["Zone_Name"].ToString();
                        link.NavigateUrl = url;
                    }
                }
                else
                {
                    e.Item.Visible = false;
                    return;
                }
            }
        }

        protected void lik_logout_Click(object sender, EventArgs e)
        {
            AuthenticateUtility.LogoutUser();
            Response.Redirect("/", true);
        }
    }
}